package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    private static final Logger log = LoggerFactory.getLogger(DishServiceImpl.class);
    @Autowired
    private DishMapper dishMapper;

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        log.info("service:删除员工：{}",ids);
        dishMapper.delete(ids);
        //删除对应菜品口味数据
        dishMapper.deleteFlavor(ids);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        log.info("service:更新菜品状态开始");
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        log.info("service:查询菜品");
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        log.info("查询条件：{}",dish);
        List<Dish> select = dishMapper.select(dish);
        log.info("查询结果：{}",select);
        List<DishVO> dishVOList = new ArrayList<>();

        //遍历查询结果
        select.forEach(selectdish -> {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(selectdish,dishVO);
            dishVOList.add(dishVO);
        });

        //复制不支持将列表赋给另一个列表
//        BeanUtils.copyProperties(select,dishVOList);
        log.info("查询结果：{}",dishVOList);
        return dishVOList;

    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        log.info("service:查询菜品数据id：{}", id);
        DishVO dishVO = new DishVO();
        //为dishVo添加数据
        Dish dish = dishMapper.getDishById(id);
        log.info("dishService:查询菜品数据：{}", dish);
        dishVO.setId(dish.getId());
        dishVO.setName(dish.getName());
        dishVO.setCategoryId(dish.getCategoryId());
        dishVO.setPrice(dish.getPrice());
        dishVO.setImage(dish.getImage());
        dishVO.setDescription(dish.getDescription());
        dishVO.setStatus(dish.getStatus());
        dishVO.setCategoryName(dishMapper.getCategoryNameById(dish.getCategoryId()));
        log.info("dishService:查询菜品数据：{}", dishVO);
        List<DishFlavor> dishFlavors=dishMapper.getDishFlavorByDishId(id);
        dishVO.setFlavors(dishFlavors);
        log.info("dishService:查询菜品数据：{}", dishVO);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        //修改菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //修改菜品口味数据

        //将菜品口味信息删除
        //因为是List类型，所有创建集合
        List<Long> dishIds = new ArrayList<>();
        dishIds.add(dishDTO.getId());
        dishMapper.deleteFlavor(dishIds);
        //再将添加的菜品信息添加进去

        //要得到菜品id然后为菜品口味添加菜品id外键
        Long dishId = dishDTO.getId();
        List<DishFlavor> dishFlavors = new ArrayList<>();

        dishDTO.getFlavors().forEach(dishFlavor -> {
                    dishFlavor.setDishId(dishId);
                    dishFlavors.add(dishFlavor);//为菜品口味集合添加数据

                }
        );
        //再将菜品口味数据添加进去
        log.info("dishService:修改菜品口味数据：{}", dishFlavors);
        dishMapper.insertDishFlavor(dishFlavors);
    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        List<DishVO> list=dishMapper.page(dishPageQueryDTO);
        Page<DishVO> page = (Page<DishVO>) list;

        list.forEach(dishVO -> {
            String categoryNameById = dishMapper.getCategoryNameById(dishVO.getCategoryId());
            log.info("employeeService:员工分页查询，结果：{}", categoryNameById);
            dishVO.setCategoryName(categoryNameById);
        });
        log.info("employeeService:员工分页查询，结果：{}", list);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        // 保存菜品数据
        Dish dish = new Dish();
        dish.setName(dishDTO.getName());
        dish.setCategoryId(dishDTO.getCategoryId());
        dish.setPrice(dishDTO.getPrice());
        dish.setStatus(StatusConstant.ENABLE);
        dish.setImage(dishDTO.getImage());
        dish.setDescription(dishDTO.getDescription());
        log.info("dishService:保存菜品数据：{}", dish);
        dishMapper.insert(dish);
        log.info("dishService:保存菜品数据成功：{}", dish);
        //保存菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                log.info("菜品的id：{}",dish.getId());
                flavor.setDishId(dish.getId());
            }
            log.info("dishService:保存菜品口味数据：{}", flavors);
            dishMapper.insertDishFlavor(flavors);
        }
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.select(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getDishFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
